<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Services\Admin\Classroom;
use App\Services\Admin\Store;
use App\Http\Requests\Admin\ClassroomController\doRule;

class ClassroomController extends Controller
{
    public function index()
    {
        $result = [
            'result' => Classroom::lists()->paginate(6),
            'select_store' => Store::where_lists(),
            'store_id' => \request()->get('store_id'),
            'sear_value' => \request()->get('sear_value'),
        ];

        return view('admin.classroom.index', $result);
    }

    public function add()
    {

        $result = [
            'select_store' => Store::where_lists(),
            'select_id' => '',
        ];

        return view('admin.classroom.edit', $result);
    }

    public function edit(Request $request, $id)
    {

        $info = Classroom::info($id);

        $result = [
            'id' => $id,
            'select_store' => Store::where_lists(),
            'select_id' => $info->store_id,
            'result' => $info,
        ];

        return view('admin.classroom.edit', $result);

    }

    public function do(doRule $request)
    {
        if ($request->ajax()) {

            $input = $request->all();

            $image_avatar = !empty($request->file('image')) ? $request->file('image') : [];

            if (!empty($image_avatar)) {
                $path = $image_avatar->store('/images/'. date('Ymd'));
                $input['image'] = "/upload/".$path;
            } else {
                unset($input['image']);
            }


            if (empty($input['id'])) {
                unset($input['id']);
                Classroom::add($input);
            } else {
                Classroom::edit($input['id'], $input);
            }

            return self::success([]);
        }
    }
}
